import { finishStagePath } from "../utils/pathUtils";
import UseSetting from "/src/common/pinias/UseSetting";
import pinia from "/src/common/pinias/pinia";
import { RemoveMethods } from "@/types/remove-methods";
import { StateMoal } from "@model/StateMoal";
import { TaskData } from "@model/TaskList";
import { FieldTree } from "@model/fieldTree";
import { TimeLogModel } from "@model/stage";

const { getCacheModel } = UseSetting(pinia);

export class FinishStage {
  id: number;
  name: string;
  lastStartTime: number; // 上次开始时间
  fatherId: number;
  startTime: Array<TimeLogModel>;
  markDownFile: string;
  TaskBasedData: Array<TaskData>;
  ClockBasedData: Array<TaskData>;

  static getTaskDataList(finishStage: FinishStage, type: number) {
    if (type === 1) return finishStage.TaskBasedData;
    if (type === 2) return finishStage.ClockBasedData;
    if (type === 3) return [...finishStage.TaskBasedData, ...finishStage.ClockBasedData];
    return new Array<TaskData>();
  }
  static getTaskData(finishStage: FinishStage, taskId: number) {
    const taskData = finishStage.TaskBasedData.find(value => value.id === taskId);
    if (taskData !== undefined) return taskData;
    else return finishStage.ClockBasedData.find(value => value.id === taskId);
  }

  constructor(
    id: number,
    name: string,
    lastStartTime: number,
    fatherId: number,
    startTime: Array<TimeLogModel>,
    markDownFile: string,
    TaskBasedData: Array<TaskData>,
    ClockBasedData: Array<TaskData>,
  ) {
    this.id = id;
    this.name = name;
    this.lastStartTime = lastStartTime;
    this.fatherId = fatherId;
    this.startTime = startTime;
    this.markDownFile = markDownFile;
    this.TaskBasedData = TaskBasedData;
    this.ClockBasedData = ClockBasedData;
  }
}
export class FinishStageList {
  id: string; // fieldId;
  finishStageList: Array<FinishStage>;

  constructor(id: string) {
    this.id = id;
    this.finishStageList = [];
  }
  static getFinishStage(finishStageList: FinishStageList, id: number) {
    if (finishStageList !== undefined) return finishStageList.finishStageList.find(value => value.id === id);
    return undefined;
  }
  static addFinishStage(finishStageList: FinishStageList, finishStage: FinishStage) {
    if (finishStage !== undefined) finishStageList.finishStageList.push(finishStage);
  }
  static deleteFinishStage(finishStageList: FinishStageList, stageId: number) {
    if (stageId !== undefined)
      finishStageList.finishStageList = finishStageList.finishStageList.filter(value => value.id !== stageId);
  }
}
export class CacheFinishStageMap {
  cacheFinishStageFileList: Array<string>; // fieldId.json
  cacheFinishStageMap: Map<string, FinishStageList>; // fieldId:[..]
  changeKeys: Array<string>; //fieldId

  setInfo(data: RemoveMethods<CacheFinishStageMap>) {
    Object.assign(this, data);
  }

  constructor(
    cacheFinishStageFileList: Array<string>,
    cacheFinishStageMap: Map<string, FinishStageList>,
    changeKeys: Array<string>,
  ) {
    this.cacheFinishStageFileList = cacheFinishStageFileList;
    this.cacheFinishStageMap = cacheFinishStageMap;
    this.changeKeys = changeKeys;
  }
  async checkCanDeleteFieldId(id: number | undefined, fieldTree: FieldTree) {
    if (id === undefined) return true;
    const result = new Set<number>();
    const data = fieldTree.fieldData.find(value => value.id === id);
    if (data !== undefined) StateMoal.getAllId(data.stageIds, result, true, true, true);
    if (result.size === 0) {
      const finishStageList = await this.getFinishStageList(id);
      return finishStageList.finishStageList.length === 0;
    } else return false;
  }
  async getFinishStageList(fieldId: number | string) {
    let finishStageList = this.cacheFinishStageMap.get(fieldId.toString());
    if (finishStageList === undefined) {
      const fileName = fieldId.toString() + ".json";
      const file = this.cacheFinishStageFileList.find(value => value === fileName);
      // console.log(stat,"getTakeTime",id,this,fileName,file)
      if (file !== undefined) {
        // 说明有保存
        finishStageList = await this.getFinishStageListCreate(fieldId);
      } else finishStageList = new FinishStageList(fieldId.toString());
    }
    return finishStageList;
  }
  async getFinishStageListCreate(fieldId: number | string) {
    // 获取数据库记录，：如果超过年份，则创建文件
    const key = fieldId.toString();
    let finishStageList = new FinishStageList(key);
    // console.log(endTime)
    finishStageList = await getCacheModel(
      finishStagePath + key + ".json",
      key,
      this.cacheFinishStageMap,
      finishStageList,
    );
    return finishStageList;
  }
}
